/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Catalog.java
 *
 * Created on 16-dec-2011, 11:43:01
 */
package view.manage.inventory;

import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import model.Product;
import main.WinkelApplication;

/**
 *
 * @author Robbie
 */
public class Catalog extends javax.swing.JPanel {

    /** Creates new form Catalog */
   /* public String sortBy;
    public String order;
    private final String ASCEND = "ASC";
    private final String DESCEND = "DESC";
    private final String DEFAULTSEARCH = "Productnaam";
*/
    public Catalog() {
       // sortBy = "Productnaam";
       // order = this.ASCEND;

        initComponents();
        setProductlist();

        //Set the mouselistener for the header in the table.
        jTableCatalog.getTableHeader().addMouseListener(new java.awt.event.MouseAdapter() {

            public void mouseClicked(java.awt.event.MouseEvent evt) {
              //  jTableHeaderMouseClicked(evt);
            }
        });
    }

    private void setProductlist() {
        
        //Contruct catalog table
        
        ((DefaultTableModel)jTableCatalog.getModel()).setNumRows(0);
        List<Product> products = WinkelApplication.getQueryManager().getProductList();
        DefaultTableModel model = (DefaultTableModel) this.jTableCatalog.getModel();
        for (Product product : products) {
             model.addRow(new Object[]{new Integer(product.getProductId()),
                        product.getName(),
                        product.getPrice(),
                        product.getDescription(),           
                        product.getCategorieId()});
             
        }
    }
    

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    /*private void jTableHeaderMouseClicked(java.awt.event.MouseEvent evt) {
        //Gets the name of the column at the mouse position.
        int column = jTableCatalog.getTableHeader().columnAtPoint(jTableCatalog.getTableHeader().getMousePosition());
        String columnName = jTableCatalog.getColumnName(column);

        //Determine the order for sorting.
        if (!sortBy.equals(columnName)) {
            //Make sure the order equels ASCEND
            order = this.ASCEND;
            sortBy = columnName;
        } else if (order.equals(this.ASCEND)) {
            order = this.DESCEND;
        } else {
            order = this.ASCEND;
        }

        setProductlist();
    }
*/
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane = new javax.swing.JScrollPane();
        jTableCatalog = new javax.swing.JTable();
        jButtonBack = new javax.swing.JButton();
        jButtonAdd = new javax.swing.JButton();
        jButtonEdit = new javax.swing.JButton();

        setPreferredSize(new java.awt.Dimension(721, 336));

        jTableCatalog.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Product_id", "Naam", "Prijs", "Omschrijving", "Categorie_id"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.String.class, java.lang.Double.class, java.lang.String.class, java.lang.Integer.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        jScrollPane.setViewportView(jTableCatalog);

        jButtonBack.setText("Terug");
        jButtonBack.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonBackActionPerformed(evt);
            }
        });

        jButtonAdd.setText("Voeg product toe");
        jButtonAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonAddActionPerformed(evt);
            }
        });

        jButtonEdit.setText("Pas product aan");
        jButtonEdit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonEditActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 684, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButtonBack, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jButtonEdit, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButtonAdd, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(43, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(26, 26, 26)
                .addComponent(jScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 361, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonBack)
                    .addComponent(jButtonAdd)
                    .addComponent(jButtonEdit))
                .addContainerGap(57, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents


private void jButtonAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonAddActionPerformed
WinkelApplication.window.newWindow(new view.manage.inventory.AddProduct());
}//GEN-LAST:event_jButtonAddActionPerformed


private void jButtonBackActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonBackActionPerformed
  WinkelApplication.getInstance().showPanel(new view.manage.Main());
}//GEN-LAST:event_jButtonBackActionPerformed

private void jButtonEditActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonEditActionPerformed
       
        //Check the selection in the table before proceding, if selected value =/ 1 -> error
    
        if(jTableCatalog.getSelectedRows().length == 1) {
            int product_id = Integer.parseInt(jTableCatalog.getValueAt(jTableCatalog.getSelectedRow(), 0).toString());
            WinkelApplication.window.newWindow(new EditProduct(product_id));
        } else {
            JOptionPane.showMessageDialog(WinkelApplication.getInstance().getMainWindow(), "Selecteer één product.", "Melding", JOptionPane.INFORMATION_MESSAGE);
        }
}//GEN-LAST:event_jButtonEditActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonAdd;
    private javax.swing.JButton jButtonBack;
    private javax.swing.JButton jButtonEdit;
    private javax.swing.JScrollPane jScrollPane;
    private javax.swing.JTable jTableCatalog;
    // End of variables declaration//GEN-END:variables
}
